var express = require('express');
var router = express.Router();
let myQuery = require('../tools/db');
let {apiUrl, jwtSuo} = require('../tools/config');
let {jwtVerify} = require('../tools/token')
let timeStamp = require('time-stamp');

// 课程详情
router.get('/courseinfo/:cid',async (req,res,next) =>{
    let {cid} = req.params;
    
    if(!cid){
        next({code:400,msg:'请传必要参数'});
        return;
    }
    
    let sql = `SELECT CONCAT('${apiUrl}','course/',image_src) AS image_src,title,price,intro,area_name,grade_name,tid,subject_name
    FROM e_course WHERE cid='${cid}';`;
    
    let [err,result] = await myQuery(sql);

    if(err){
         next({code:500,msg:err});
         return;
    }
    
    let [{tid}] = result;
    let sql1 = `SELECT realname,intro,CONCAT('${apiUrl}',head_photo_url) AS head_photo_url FROM e_teacher WHERE tid='${tid}';`;
    
    let [err1,result1]  = await myQuery(sql1);
    if(err1){
        next({code:500,msg:err1});
        return;
   }
   
   let sql2 = `SELECT * FROM e_video WHERE cid='${cid}';`;
   
   let [err2,result2] = await myQuery(sql2);
   if(err2){
        next({code:500,msg:err2});
        return;
   }

   res.send({code:200,msg:'ok',result:{course:result[0],teacher:result1[0],video:result2}})
 
})

// 验证token的中间件
router.use((req,res,next) =>{
     let token = req.headers.authorization;
     let s = jwtVerify(token,jwtSuo);
      
     if(!s){
          next({code:400,msg:'请先登录'});
          return;
     }
     
     req.mid = s.mid;
     next();
})

// 报名接口
router.get('/study',async (req,res,next) =>{
      let {cid} = req.query;
      console.log(cid);
      if(!cid){
          next({code:400,msg:'请传必要参数'});
          return;
      }

      let sql = `SELECT * FROM e_order WHERE cid='${cid}' AND mid= '${req.mid}';`;
      let [err,result]  = await myQuery(sql);
      if(err){
          next({code:500,msg:err});
          return;
     }

     if(result.length >0){
         next({code:400,msg:'已报名该课程'})
         return;
     }
     let oid = timeStamp('YYYYMMDDHHmmss') + (Math.random() + '').slice(2,8);
     let createdate = Date.now();
     
     let sql1 = `INSERT INTO e_order (oid,cid,mid,createdate,status) VALUES ('${oid}','${cid}','${req.mid}','${createdate}',1)`;
     
     let [err1,result1] = await myQuery(sql1);
     if(err1){
          next({code:500,msg:err1});
          return;
     }

     if(result1.insertId){
          res.send({code:200,msg:'报名成功'});
     }else{
          next({code:500,msg:'请稍后再试'});
          return;
     }
})

// 视频地址接口
router.get('/video', async (req,res,next) =>{
     let {id} = req.query;
     if(!id){
          next({code:400,msg:'请传必要参数'});
          return; 
     }

     let sql = `SELECT video_title,vnum,cid,CONCAT('${apiUrl}','video/',video_src) AS video_src FROM  e_video WHERE id='${id}';`;

     let [err,result]  = await myQuery(sql);
     if(err){
          next({code:500,msg:err});
          return;
     }

     res.send({code:200,msg:'ok',result});
})


module.exports = router;